package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台管理用户设置
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            //从security中获取user对象信息.因为在之前就已经把对应的user用户信息存放进Security的user中了
            //现在可以直接取出用户的所有存放的信息
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //System.out.println("user ============= " + user);

            //获取到用户信息之后,拿出用户的user中的username,返回给前端页面
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findByPage(queryPageBean);
        return pageResult;
    }

    //添加用户
    @RequestMapping("/addUser/{roleIds}")
    @PreAuthorize("hasAuthority('USER_ADD')")//添加用户信息权限
    public Result addUser(@RequestBody com.jsz.pojo.User user, @PathVariable Integer[] roleIds) {
        try {
            userService.addUser(user, roleIds);
            //添加成功
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    //根据Id查询用户信息
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('USER_QUERY')")//查询用户信息权限
    public Result findById(Integer id) {
        try {
            com.jsz.pojo.User user = userService.findById(id);
            //将密码设置为空,不让前端展示密码
            if (user != null) {
                user.setPassword("*****");
            }
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //根据用户ID查询中间关系表,查询用户对于的角色信息
    @PreAuthorize("hasAuthority('USER_QUERY')")//查询用户信息权限
    @RequestMapping("/findRoleIdsByUserId/{id}")
    public Result findRoleIdsByUserId(@PathVariable Integer id) {
        try {
            List<Integer> list = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //删除用户
    @RequestMapping("/deleteUserById")
    @PreAuthorize("hasAuthority('USER_DELETE')")//删除用户信息权限
    public Result deleteUserById(Integer id) {
        try {
            userService.deleteUserById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    //编辑数据
    @RequestMapping("/update/{roleIds}")
    @PreAuthorize("hasAuthority('USER_EDIT')")//修改用户信息权限
    public Result update(@RequestBody com.jsz.pojo.User user, @PathVariable Integer[] roleIds) {
        try {
            userService.update(user, roleIds);
            return new Result(true, MessageConstant.UPDATE_USER_SUCCESS);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_USER_FAIL);
        }
    }
}
